home *** CD-ROM | disk | FTP | other *** search
- #include <Dialogs.h>
- #include <Fonts.h>
- #include <Quickdraw.h>
- #include <Windows.h>
-
- #include <NetSprocket.h>
-
- #define CopyPStr(dst,src) BlockMove(src,dst,src[0]+1)
-
-
- enum {
- kPlayerLocation = 0
- };
-
- typedef struct PlayerLocationMessage {
- NSpMessageHeader header;
- Point where;
- } PlayerLocationMessage;
-
-
- typedef struct PlayerLocation {
- struct PlayerLocation* next;
- struct PlayerLocation* prev;
- NSpPlayerID id;
- Str31 name;
- Point where;
- } PlayerLocation;
-
-
- WindowPtr gMyWindow = NULL;
- NSpGameReference gMyGame = NULL;
- Boolean gRunning = false;
- Boolean gChanged = false;
- PlayerLocation* gPlayerList = NULL;
- Boolean gHost;
-
- void main(void);
- static void Init(void);
- static void HandleError(void);
- static void DoHostGame(void);
- static void DoJoinGame(void);
- static void DoHandleEvents(void);
- static void DoHandleMessages(void);
- static void DoJoinApproved(NSpJoinApprovedMessage* message);
- static void DoPlayerJoined(NSpPlayerJoinedMessage* message);
- static void DoPlayerLeft(NSpPlayerLeftMessage* message);
- static void DoPlayerLocation(PlayerLocationMessage* message);
- static void UpdateLocation(Point where);
- static void Redraw(void);
- static PlayerLocation* FindPlayer(NSpPlayerID id);
-
-
- void main(void)
- {
- Init();
-
- // Let them host
- DoHostGame();
-
- // If not hosting, let them join
- if (gMyGame == NULL)
- {
- DoJoinGame();
- }
-
- // Track the action
- if (gMyGame != NULL)
- {
- gRunning = true;
-
- while (gRunning)
- {
- DoHandleEvents();
- DoHandleMessages();
- Redraw();
- }
- }
- }
-
-
- void Init(void)
- {
- OSStatus err;
- Rect bounds;
-
- InitGraf(&qd.thePort);
- InitFonts();
- InitWindows();
- InitDialogs(NULL);
- InitCursor();
- InitMenus();
- TEInit();
-
- bounds.top = 60;
- bounds.left = 20;
- bounds.bottom = bounds.top+240;
- bounds.right = bounds.left+320;
-
- gMyWindow = NewWindow(NULL, &bounds, "\p", true, dBoxProc, (WindowPtr) -1, false, 0);
-
- err = NSpInitialize(550, 100000, 100, 'nspx', 100);
- if (err != noErr) HandleError();
- }
-
-
- void HandleError(void)
- {
- DebugStr("\pError");
- }
-
-
- // Displays the Host dialog and creates a new game (NULL if cancelled)
- void DoHostGame(void)
- {
- OSStatus err;
- Boolean ok;
- Str31 gameName;
- Str31 playerName;
- Str31 password;
- NSpProtocolListReference protocolList;
- NSpProtocolReference atRef, ipRef;
-
- // Do the dialog
- CopyPStr(gameName, "\pMyGame");
- CopyPStr(playerName, "\pPlayer1");
- CopyPStr(password, "\pwinbites");
-
- // Set up our protocol list
- atRef = NSpProtocol_CreateAppleTalk(gameName, "\pNetSprocketExample", 0, 0);
- ipRef = NSpProtocol_CreateIP(2002, 0, 0);
-
- err = NSpProtocolList_New(NULL, &protocolList);
- if (err != noErr) HandleError();
-
- err = NSpProtocolList_Append(protocolList, atRef);
- if (err != noErr) HandleError();
- err = NSpProtocolList_Append(protocolList, ipRef);
- if (err != noErr) HandleError();
-
- ok = NSpDoModalHostDialog(protocolList, gameName, playerName, password, NULL);
- if (!ok) return;
-
- // Make the game object, and begin advertising
- err = NSpGame_Host(&gMyGame, protocolList, 8, gameName, password, playerName, 0, kNSpClientServer, 0);
- if (err != noErr) HandleError();
-
- NSpProtocolList_Dispose(protocolList);
-
- gHost = true;
- }
-
-
- // Displays the Join dialog and creates a new game (NULL if cancelled)
- void DoJoinGame(void)
- {
- OSStatus err;
- Str31 playerName;
- Str31 password;
- NSpAddressReference address;
-
-
- CopyPStr(playerName, "\pPlayer2");
- CopyPStr(password, "\pwinbites");
-
- address = NSpDoModalJoinDialog("\pNetSprocketExample", "\pAvailable Games:", playerName, password, NULL);
-
- if (address != NULL)
- {
- err = NSpGame_Join(&gMyGame, address, playerName, password, 0, 0, NULL, 0);
- if (err != noErr) HandleError();
-
- NSpReleaseAddressReference(address);
- }
- gHost = false;
- }
-
-
- // Handles events and sends messages while the mouse is down
- void DoHandleEvents(void)
- {
- EventRecord ev;
- Point where;
-
- if (Button())
- {
- SetPort(gMyWindow);
- GetMouse(&where);
- UpdateLocation(where);
- }
-
- if (WaitNextEvent(everyEvent, &ev, 0, NULL))
- {
- switch (ev.what)
- {
- case keyDown:
- gRunning = false;
- break;
- }
- }
- }
-
-
- // Receives and parses messages
- void DoHandleMessages(void)
- {
- NSpMessageHeader* message;
-
- while ((message = NSpMessage_Get(gMyGame)) != NULL)
- {
- switch(message->what)
- {
- case kNSpJoinApproved:
- DoJoinApproved((NSpJoinApprovedMessage *) message);
- break;
- case kNSpPlayerJoined:
- DoPlayerJoined((NSpPlayerJoinedMessage*) message);
- break;
-
- case kNSpPlayerLeft:
- DoPlayerLeft((NSpPlayerLeftMessage*) message);
- break;
-
- case kPlayerLocation:
- DoPlayerLocation((PlayerLocationMessage*) message);
- break;
- }
-
- NSpMessage_Release(gMyGame, message);
- }
- }
-
-
- // Remember a new player.
- void DoPlayerJoined(NSpPlayerJoinedMessage* message)
- {
- PlayerLocation* player;
-
- if (!gHost && message->playerInfo.id == NSpPlayer_GetMyID(gMyGame))
- return;
-
- // Allocate the memory
- player = (PlayerLocation*) NewPtr(sizeof(PlayerLocation));
- if (player == NULL) HandleError();
-
- // Link it into the list
- player->next = gPlayerList;
- player->prev = NULL;
- gPlayerList = player;
-
- // Fill it in
- player->id = message->playerInfo.id;
- BlockMove(message->playerInfo.name, player->name, message->playerInfo.name[0]+1);
- player->where.v = 100;
- player->where.h = 200;
-
- // Cause re-render
- gChanged = true;
- }
-
-
-
- void DoJoinApproved(NSpJoinApprovedMessage* message)
- {
- OSStatus err;
- NSpPlayerEnumerationPtr thePlayers;
- NSpPlayerInfoPtr playerInfo;
- PlayerLocation* player;
- int i;
-
- err = NSpPlayer_GetEnumeration(gMyGame, &thePlayers);
- if (err == noErr)
- {
- for (i = 0; i < thePlayers->count; i++)
- {
- playerInfo = thePlayers->playerInfo[i];
- // Allocate the memory
- player = (PlayerLocation*) NewPtr(sizeof(PlayerLocation));
- if (player == NULL) HandleError();
-
- // Link it into the list
- player->next = gPlayerList;
- player->prev = NULL;
- gPlayerList = player;
-
- // Fill it in
- player->id = playerInfo->id;
- BlockMove(playerInfo->name, player->name, playerInfo->name[0]+1);
- player->where.v = 100;
- player->where.h = 200;
-
- // Cause re-render
- gChanged = true;
- }
-
- NSpPlayer_ReleaseEnumeration(gMyGame, thePlayers);
- }
- }
- // Get rid of a player.
- void DoPlayerLeft(NSpPlayerLeftMessage* message)
- {
- PlayerLocation* player;
- PlayerLocation* next;
- PlayerLocation* prev;
-
- // Find the player from the message sender ID
- player = FindPlayer(message->header.from);
- if (player == NULL) HandleError();
-
- // Unlink it from the list
- prev = player->prev;
- next = player->next;
-
- if (next != NULL)
- {
- next->prev = prev;
- }
-
- if (prev != NULL)
- {
- prev->next = next;
- }
- else
- {
- gPlayerList = next;
- }
-
- // Get rid of the memory
- DisposePtr((Ptr) player);
-
- // Cause re-render
- gChanged = true;
- }
-
-
- // Handles a player location message
- void DoPlayerLocation(PlayerLocationMessage* message)
- {
-
- PlayerLocation* player;
-
- // Find the player from the message sender ID
- player = FindPlayer(message->header.from);
- if (player == NULL) HandleError();
-
- // Grab the new location
- player->where = message->where;
-
- // Cause re-render
- gChanged = true;
- }
-
-
- // Sends a message indicating the new location
- void UpdateLocation(Point where)
- {
- PlayerLocationMessage message;
-
- NSpClearMessageHeader(&message.header);
-
- message.header.what = kPlayerLocation;
- message.header.to = kNSpAllPlayers;
-
- message.header.messageLen = sizeof(PlayerLocationMessage);
- message.where = where;
-
- { //••• TEMPORARY
- #if 0
- long foo;
- Delay(60, &foo);
- #endif
-
- message.header.from = NSpPlayer_GetMyID(gMyGame);
- }
-
- NSpMessage_Send(gMyGame, &message.header, kNSpSendFlag_Normal | kNSpSendFlag_SelfSend);
- }
-
-
- // Redraws the contents of the window
- void Redraw(void)
- {
- PlayerLocation* player;
-
- if (gChanged)
- {
- SetPort(gMyWindow);
- EraseRect(&gMyWindow->portRect);
-
- for (player = gPlayerList; player != NULL; player = player->next)
- {
- MoveTo(player->where.h, player->where.v);
- DrawString(player->name);
- }
-
- gChanged = false;
- }
- }
-
-
- // Returns a pointer to the structure describing the player given the ID.
- // If not found, then NULL is returned.
- PlayerLocation* FindPlayer(NSpPlayerID id)
- {
- PlayerLocation* player;
-
- for (player = gPlayerList; player != NULL; player = player->next)
- {
- if (player->id == id)
- {
- return player;
- }
- }
-
- return NULL;
- }
-
-
-